package qldc.pm.tiensu.service.persistence;

import com.liferay.portal.kernel.cache.CacheRegistryUtil;
import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderPath;
import com.liferay.portal.kernel.dao.orm.Query;
import com.liferay.portal.kernel.dao.orm.QueryPos;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.InstanceFactory;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.kernel.util.PropsUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.UnmodifiableList;
import com.liferay.portal.model.CacheModel;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;

import qldc.pm.tiensu.NoSuchTEMP_TienSuException;
import qldc.pm.tiensu.model.TEMP_TienSu;
import qldc.pm.tiensu.model.impl.TEMP_TienSuImpl;
import qldc.pm.tiensu.model.impl.TEMP_TienSuModelImpl;
import qldc.pm.tiensu.service.persistence.TEMP_TienSuPersistence;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * The persistence implementation for the t e m p_ tien su service.
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author Brian Wing Shun Chan
 * @see TEMP_TienSuPersistence
 * @see TEMP_TienSuUtil
 * @generated
 */
public class TEMP_TienSuPersistenceImpl extends BasePersistenceImpl<TEMP_TienSu>
    implements TEMP_TienSuPersistence {
    /*
     * NOTE FOR DEVELOPERS:
     *
     * Never modify or reference this class directly. Always use {@link TEMP_TienSuUtil} to access the t e m p_ tien su persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
     */
    public static final String FINDER_CLASS_NAME_ENTITY = TEMP_TienSuImpl.class.getName();
    public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
        ".List1";
    public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
        ".List2";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(TEMP_TienSuModelImpl.ENTITY_CACHE_ENABLED,
            TEMP_TienSuModelImpl.FINDER_CACHE_ENABLED, TEMP_TienSuImpl.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(TEMP_TienSuModelImpl.ENTITY_CACHE_ENABLED,
            TEMP_TienSuModelImpl.FINDER_CACHE_ENABLED, TEMP_TienSuImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
    public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(TEMP_TienSuModelImpl.ENTITY_CACHE_ENABLED,
            TEMP_TienSuModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_DOITUONGTIENSUID =
        new FinderPath(TEMP_TienSuModelImpl.ENTITY_CACHE_ENABLED,
            TEMP_TienSuModelImpl.FINDER_CACHE_ENABLED, TEMP_TienSuImpl.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByDoiTuongTienSuId",
            new String[] {
                Long.class.getName(),
                
            Integer.class.getName(), Integer.class.getName(),
                OrderByComparator.class.getName()
            });
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_DOITUONGTIENSUID =
        new FinderPath(TEMP_TienSuModelImpl.ENTITY_CACHE_ENABLED,
            TEMP_TienSuModelImpl.FINDER_CACHE_ENABLED, TEMP_TienSuImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
            "findByDoiTuongTienSuId", new String[] { Long.class.getName() },
            TEMP_TienSuModelImpl.DOITUONGTIENSUID_COLUMN_BITMASK |
            TEMP_TienSuModelImpl.CREATEDATE_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_DOITUONGTIENSUID = new FinderPath(TEMP_TienSuModelImpl.ENTITY_CACHE_ENABLED,
            TEMP_TienSuModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
            "countByDoiTuongTienSuId", new String[] { Long.class.getName() });
    private static final String _FINDER_COLUMN_DOITUONGTIENSUID_DOITUONGTIENSUID_2 =
        "temp_TienSu.doiTuongTienSuId = ?";
    private static final String _SQL_SELECT_TEMP_TIENSU = "SELECT temp_TienSu FROM TEMP_TienSu temp_TienSu";
    private static final String _SQL_SELECT_TEMP_TIENSU_WHERE = "SELECT temp_TienSu FROM TEMP_TienSu temp_TienSu WHERE ";
    private static final String _SQL_COUNT_TEMP_TIENSU = "SELECT COUNT(temp_TienSu) FROM TEMP_TienSu temp_TienSu";
    private static final String _SQL_COUNT_TEMP_TIENSU_WHERE = "SELECT COUNT(temp_TienSu) FROM TEMP_TienSu temp_TienSu WHERE ";
    private static final String _ORDER_BY_ENTITY_ALIAS = "temp_TienSu.";
    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No TEMP_TienSu exists with the primary key ";
    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No TEMP_TienSu exists with the key {";
    private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = GetterUtil.getBoolean(PropsUtil.get(
                PropsKeys.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE));
    private static Log _log = LogFactoryUtil.getLog(TEMP_TienSuPersistenceImpl.class);
    private static TEMP_TienSu _nullTEMP_TienSu = new TEMP_TienSuImpl() {
            @Override
            public Object clone() {
                return this;
            }

            @Override
            public CacheModel<TEMP_TienSu> toCacheModel() {
                return _nullTEMP_TienSuCacheModel;
            }
        };

    private static CacheModel<TEMP_TienSu> _nullTEMP_TienSuCacheModel = new CacheModel<TEMP_TienSu>() {
            @Override
            public TEMP_TienSu toEntityModel() {
                return _nullTEMP_TienSu;
            }
        };

    public TEMP_TienSuPersistenceImpl() {
        setModelClass(TEMP_TienSu.class);
    }

    /**
     * Returns all the t e m p_ tien sus where doiTuongTienSuId = &#63;.
     *
     * @param doiTuongTienSuId the doi tuong tien su ID
     * @return the matching t e m p_ tien sus
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<TEMP_TienSu> findByDoiTuongTienSuId(long doiTuongTienSuId)
        throws SystemException {
        return findByDoiTuongTienSuId(doiTuongTienSuId, QueryUtil.ALL_POS,
            QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the t e m p_ tien sus where doiTuongTienSuId = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link qldc.pm.tiensu.model.impl.TEMP_TienSuModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param doiTuongTienSuId the doi tuong tien su ID
     * @param start the lower bound of the range of t e m p_ tien sus
     * @param end the upper bound of the range of t e m p_ tien sus (not inclusive)
     * @return the range of matching t e m p_ tien sus
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<TEMP_TienSu> findByDoiTuongTienSuId(long doiTuongTienSuId,
        int start, int end) throws SystemException {
        return findByDoiTuongTienSuId(doiTuongTienSuId, start, end, null);
    }

    /**
     * Returns an ordered range of all the t e m p_ tien sus where doiTuongTienSuId = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link qldc.pm.tiensu.model.impl.TEMP_TienSuModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param doiTuongTienSuId the doi tuong tien su ID
     * @param start the lower bound of the range of t e m p_ tien sus
     * @param end the upper bound of the range of t e m p_ tien sus (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching t e m p_ tien sus
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<TEMP_TienSu> findByDoiTuongTienSuId(long doiTuongTienSuId,
        int start, int end, OrderByComparator orderByComparator)
        throws SystemException {
        boolean pagination = true;
        FinderPath finderPath = null;
        Object[] finderArgs = null;

        if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
                (orderByComparator == null)) {
            pagination = false;
            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_DOITUONGTIENSUID;
            finderArgs = new Object[] { doiTuongTienSuId };
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_DOITUONGTIENSUID;
            finderArgs = new Object[] {
                    doiTuongTienSuId,
                    
                    start, end, orderByComparator
                };
        }

        List<TEMP_TienSu> list = (List<TEMP_TienSu>) FinderCacheUtil.getResult(finderPath,
                finderArgs, this);

        if ((list != null) && !list.isEmpty()) {
            for (TEMP_TienSu temp_TienSu : list) {
                if ((doiTuongTienSuId != temp_TienSu.getDoiTuongTienSuId())) {
                    list = null;

                    break;
                }
            }
        }

        if (list == null) {
            StringBundler query = null;

            if (orderByComparator != null) {
                query = new StringBundler(3 +
                        (orderByComparator.getOrderByFields().length * 3));
            } else {
                query = new StringBundler(3);
            }

            query.append(_SQL_SELECT_TEMP_TIENSU_WHERE);

            query.append(_FINDER_COLUMN_DOITUONGTIENSUID_DOITUONGTIENSUID_2);

            if (orderByComparator != null) {
                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
                    orderByComparator);
            } else
             if (pagination) {
                query.append(TEMP_TienSuModelImpl.ORDER_BY_JPQL);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                qPos.add(doiTuongTienSuId);

                if (!pagination) {
                    list = (List<TEMP_TienSu>) QueryUtil.list(q, getDialect(),
                            start, end, false);

                    Collections.sort(list);

                    list = new UnmodifiableList<TEMP_TienSu>(list);
                } else {
                    list = (List<TEMP_TienSu>) QueryUtil.list(q, getDialect(),
                            start, end);
                }

                cacheResult(list);

                FinderCacheUtil.putResult(finderPath, finderArgs, list);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return list;
    }

    /**
     * Returns the first t e m p_ tien su in the ordered set where doiTuongTienSuId = &#63;.
     *
     * @param doiTuongTienSuId the doi tuong tien su ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching t e m p_ tien su
     * @throws qldc.pm.tiensu.NoSuchTEMP_TienSuException if a matching t e m p_ tien su could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TEMP_TienSu findByDoiTuongTienSuId_First(long doiTuongTienSuId,
        OrderByComparator orderByComparator)
        throws NoSuchTEMP_TienSuException, SystemException {
        TEMP_TienSu temp_TienSu = fetchByDoiTuongTienSuId_First(doiTuongTienSuId,
                orderByComparator);

        if (temp_TienSu != null) {
            return temp_TienSu;
        }

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("doiTuongTienSuId=");
        msg.append(doiTuongTienSuId);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchTEMP_TienSuException(msg.toString());
    }

    /**
     * Returns the first t e m p_ tien su in the ordered set where doiTuongTienSuId = &#63;.
     *
     * @param doiTuongTienSuId the doi tuong tien su ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching t e m p_ tien su, or <code>null</code> if a matching t e m p_ tien su could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TEMP_TienSu fetchByDoiTuongTienSuId_First(long doiTuongTienSuId,
        OrderByComparator orderByComparator) throws SystemException {
        List<TEMP_TienSu> list = findByDoiTuongTienSuId(doiTuongTienSuId, 0, 1,
                orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the last t e m p_ tien su in the ordered set where doiTuongTienSuId = &#63;.
     *
     * @param doiTuongTienSuId the doi tuong tien su ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching t e m p_ tien su
     * @throws qldc.pm.tiensu.NoSuchTEMP_TienSuException if a matching t e m p_ tien su could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TEMP_TienSu findByDoiTuongTienSuId_Last(long doiTuongTienSuId,
        OrderByComparator orderByComparator)
        throws NoSuchTEMP_TienSuException, SystemException {
        TEMP_TienSu temp_TienSu = fetchByDoiTuongTienSuId_Last(doiTuongTienSuId,
                orderByComparator);

        if (temp_TienSu != null) {
            return temp_TienSu;
        }

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("doiTuongTienSuId=");
        msg.append(doiTuongTienSuId);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchTEMP_TienSuException(msg.toString());
    }

    /**
     * Returns the last t e m p_ tien su in the ordered set where doiTuongTienSuId = &#63;.
     *
     * @param doiTuongTienSuId the doi tuong tien su ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching t e m p_ tien su, or <code>null</code> if a matching t e m p_ tien su could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TEMP_TienSu fetchByDoiTuongTienSuId_Last(long doiTuongTienSuId,
        OrderByComparator orderByComparator) throws SystemException {
        int count = countByDoiTuongTienSuId(doiTuongTienSuId);

        if (count == 0) {
            return null;
        }

        List<TEMP_TienSu> list = findByDoiTuongTienSuId(doiTuongTienSuId,
                count - 1, count, orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the t e m p_ tien sus before and after the current t e m p_ tien su in the ordered set where doiTuongTienSuId = &#63;.
     *
     * @param tienSuId the primary key of the current t e m p_ tien su
     * @param doiTuongTienSuId the doi tuong tien su ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the previous, current, and next t e m p_ tien su
     * @throws qldc.pm.tiensu.NoSuchTEMP_TienSuException if a t e m p_ tien su with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TEMP_TienSu[] findByDoiTuongTienSuId_PrevAndNext(long tienSuId,
        long doiTuongTienSuId, OrderByComparator orderByComparator)
        throws NoSuchTEMP_TienSuException, SystemException {
        TEMP_TienSu temp_TienSu = findByPrimaryKey(tienSuId);

        Session session = null;

        try {
            session = openSession();

            TEMP_TienSu[] array = new TEMP_TienSuImpl[3];

            array[0] = getByDoiTuongTienSuId_PrevAndNext(session, temp_TienSu,
                    doiTuongTienSuId, orderByComparator, true);

            array[1] = temp_TienSu;

            array[2] = getByDoiTuongTienSuId_PrevAndNext(session, temp_TienSu,
                    doiTuongTienSuId, orderByComparator, false);

            return array;
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    protected TEMP_TienSu getByDoiTuongTienSuId_PrevAndNext(Session session,
        TEMP_TienSu temp_TienSu, long doiTuongTienSuId,
        OrderByComparator orderByComparator, boolean previous) {
        StringBundler query = null;

        if (orderByComparator != null) {
            query = new StringBundler(6 +
                    (orderByComparator.getOrderByFields().length * 6));
        } else {
            query = new StringBundler(3);
        }

        query.append(_SQL_SELECT_TEMP_TIENSU_WHERE);

        query.append(_FINDER_COLUMN_DOITUONGTIENSUID_DOITUONGTIENSUID_2);

        if (orderByComparator != null) {
            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();

            if (orderByConditionFields.length > 0) {
                query.append(WHERE_AND);
            }

            for (int i = 0; i < orderByConditionFields.length; i++) {
                query.append(_ORDER_BY_ENTITY_ALIAS);
                query.append(orderByConditionFields[i]);

                if ((i + 1) < orderByConditionFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN_HAS_NEXT);
                    } else {
                        query.append(WHERE_LESSER_THAN_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN);
                    } else {
                        query.append(WHERE_LESSER_THAN);
                    }
                }
            }

            query.append(ORDER_BY_CLAUSE);

            String[] orderByFields = orderByComparator.getOrderByFields();

            for (int i = 0; i < orderByFields.length; i++) {
                query.append(_ORDER_BY_ENTITY_ALIAS);
                query.append(orderByFields[i]);

                if ((i + 1) < orderByFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC_HAS_NEXT);
                    } else {
                        query.append(ORDER_BY_DESC_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC);
                    } else {
                        query.append(ORDER_BY_DESC);
                    }
                }
            }
        } else {
            query.append(TEMP_TienSuModelImpl.ORDER_BY_JPQL);
        }

        String sql = query.toString();

        Query q = session.createQuery(sql);

        q.setFirstResult(0);
        q.setMaxResults(2);

        QueryPos qPos = QueryPos.getInstance(q);

        qPos.add(doiTuongTienSuId);

        if (orderByComparator != null) {
            Object[] values = orderByComparator.getOrderByConditionValues(temp_TienSu);

            for (Object value : values) {
                qPos.add(value);
            }
        }

        List<TEMP_TienSu> list = q.list();

        if (list.size() == 2) {
            return list.get(1);
        } else {
            return null;
        }
    }

    /**
     * Removes all the t e m p_ tien sus where doiTuongTienSuId = &#63; from the database.
     *
     * @param doiTuongTienSuId the doi tuong tien su ID
     * @throws SystemException if a system exception occurred
     */
    @Override
    public void removeByDoiTuongTienSuId(long doiTuongTienSuId)
        throws SystemException {
        for (TEMP_TienSu temp_TienSu : findByDoiTuongTienSuId(
                doiTuongTienSuId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
            remove(temp_TienSu);
        }
    }

    /**
     * Returns the number of t e m p_ tien sus where doiTuongTienSuId = &#63;.
     *
     * @param doiTuongTienSuId the doi tuong tien su ID
     * @return the number of matching t e m p_ tien sus
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countByDoiTuongTienSuId(long doiTuongTienSuId)
        throws SystemException {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_DOITUONGTIENSUID;

        Object[] finderArgs = new Object[] { doiTuongTienSuId };

        Long count = (Long) FinderCacheUtil.getResult(finderPath, finderArgs,
                this);

        if (count == null) {
            StringBundler query = new StringBundler(2);

            query.append(_SQL_COUNT_TEMP_TIENSU_WHERE);

            query.append(_FINDER_COLUMN_DOITUONGTIENSUID_DOITUONGTIENSUID_2);

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                qPos.add(doiTuongTienSuId);

                count = (Long) q.uniqueResult();

                FinderCacheUtil.putResult(finderPath, finderArgs, count);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    /**
     * Caches the t e m p_ tien su in the entity cache if it is enabled.
     *
     * @param temp_TienSu the t e m p_ tien su
     */
    @Override
    public void cacheResult(TEMP_TienSu temp_TienSu) {
        EntityCacheUtil.putResult(TEMP_TienSuModelImpl.ENTITY_CACHE_ENABLED,
            TEMP_TienSuImpl.class, temp_TienSu.getPrimaryKey(), temp_TienSu);

        temp_TienSu.resetOriginalValues();
    }

    /**
     * Caches the t e m p_ tien sus in the entity cache if it is enabled.
     *
     * @param temp_TienSus the t e m p_ tien sus
     */
    @Override
    public void cacheResult(List<TEMP_TienSu> temp_TienSus) {
        for (TEMP_TienSu temp_TienSu : temp_TienSus) {
            if (EntityCacheUtil.getResult(
                        TEMP_TienSuModelImpl.ENTITY_CACHE_ENABLED,
                        TEMP_TienSuImpl.class, temp_TienSu.getPrimaryKey()) == null) {
                cacheResult(temp_TienSu);
            } else {
                temp_TienSu.resetOriginalValues();
            }
        }
    }

    /**
     * Clears the cache for all t e m p_ tien sus.
     *
     * <p>
     * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
     * </p>
     */
    @Override
    public void clearCache() {
        if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
            CacheRegistryUtil.clear(TEMP_TienSuImpl.class.getName());
        }

        EntityCacheUtil.clearCache(TEMP_TienSuImpl.class.getName());

        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    }

    /**
     * Clears the cache for the t e m p_ tien su.
     *
     * <p>
     * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
     * </p>
     */
    @Override
    public void clearCache(TEMP_TienSu temp_TienSu) {
        EntityCacheUtil.removeResult(TEMP_TienSuModelImpl.ENTITY_CACHE_ENABLED,
            TEMP_TienSuImpl.class, temp_TienSu.getPrimaryKey());

        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    }

    @Override
    public void clearCache(List<TEMP_TienSu> temp_TienSus) {
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

        for (TEMP_TienSu temp_TienSu : temp_TienSus) {
            EntityCacheUtil.removeResult(TEMP_TienSuModelImpl.ENTITY_CACHE_ENABLED,
                TEMP_TienSuImpl.class, temp_TienSu.getPrimaryKey());
        }
    }

    /**
     * Creates a new t e m p_ tien su with the primary key. Does not add the t e m p_ tien su to the database.
     *
     * @param tienSuId the primary key for the new t e m p_ tien su
     * @return the new t e m p_ tien su
     */
    @Override
    public TEMP_TienSu create(long tienSuId) {
        TEMP_TienSu temp_TienSu = new TEMP_TienSuImpl();

        temp_TienSu.setNew(true);
        temp_TienSu.setPrimaryKey(tienSuId);

        return temp_TienSu;
    }

    /**
     * Removes the t e m p_ tien su with the primary key from the database. Also notifies the appropriate model listeners.
     *
     * @param tienSuId the primary key of the t e m p_ tien su
     * @return the t e m p_ tien su that was removed
     * @throws qldc.pm.tiensu.NoSuchTEMP_TienSuException if a t e m p_ tien su with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TEMP_TienSu remove(long tienSuId)
        throws NoSuchTEMP_TienSuException, SystemException {
        return remove((Serializable) tienSuId);
    }

    /**
     * Removes the t e m p_ tien su with the primary key from the database. Also notifies the appropriate model listeners.
     *
     * @param primaryKey the primary key of the t e m p_ tien su
     * @return the t e m p_ tien su that was removed
     * @throws qldc.pm.tiensu.NoSuchTEMP_TienSuException if a t e m p_ tien su with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TEMP_TienSu remove(Serializable primaryKey)
        throws NoSuchTEMP_TienSuException, SystemException {
        Session session = null;

        try {
            session = openSession();

            TEMP_TienSu temp_TienSu = (TEMP_TienSu) session.get(TEMP_TienSuImpl.class,
                    primaryKey);

            if (temp_TienSu == null) {
                if (_log.isWarnEnabled()) {
                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
                }

                throw new NoSuchTEMP_TienSuException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
                    primaryKey);
            }

            return remove(temp_TienSu);
        } catch (NoSuchTEMP_TienSuException nsee) {
            throw nsee;
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    @Override
    protected TEMP_TienSu removeImpl(TEMP_TienSu temp_TienSu)
        throws SystemException {
        temp_TienSu = toUnwrappedModel(temp_TienSu);

        Session session = null;

        try {
            session = openSession();

            if (!session.contains(temp_TienSu)) {
                temp_TienSu = (TEMP_TienSu) session.get(TEMP_TienSuImpl.class,
                        temp_TienSu.getPrimaryKeyObj());
            }

            if (temp_TienSu != null) {
                session.delete(temp_TienSu);
            }
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }

        if (temp_TienSu != null) {
            clearCache(temp_TienSu);
        }

        return temp_TienSu;
    }

    @Override
    public TEMP_TienSu updateImpl(qldc.pm.tiensu.model.TEMP_TienSu temp_TienSu)
        throws SystemException {
        temp_TienSu = toUnwrappedModel(temp_TienSu);

        boolean isNew = temp_TienSu.isNew();

        TEMP_TienSuModelImpl temp_TienSuModelImpl = (TEMP_TienSuModelImpl) temp_TienSu;

        Session session = null;

        try {
            session = openSession();

            if (temp_TienSu.isNew()) {
                session.save(temp_TienSu);

                temp_TienSu.setNew(false);
            } else {
                session.merge(temp_TienSu);
            }
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }

        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);

        if (isNew || !TEMP_TienSuModelImpl.COLUMN_BITMASK_ENABLED) {
            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
        }
        else {
            if ((temp_TienSuModelImpl.getColumnBitmask() &
                    FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_DOITUONGTIENSUID.getColumnBitmask()) != 0) {
                Object[] args = new Object[] {
                        temp_TienSuModelImpl.getOriginalDoiTuongTienSuId()
                    };

                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_DOITUONGTIENSUID,
                    args);
                FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_DOITUONGTIENSUID,
                    args);

                args = new Object[] { temp_TienSuModelImpl.getDoiTuongTienSuId() };

                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_DOITUONGTIENSUID,
                    args);
                FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_DOITUONGTIENSUID,
                    args);
            }
        }

        EntityCacheUtil.putResult(TEMP_TienSuModelImpl.ENTITY_CACHE_ENABLED,
            TEMP_TienSuImpl.class, temp_TienSu.getPrimaryKey(), temp_TienSu);

        return temp_TienSu;
    }

    protected TEMP_TienSu toUnwrappedModel(TEMP_TienSu temp_TienSu) {
        if (temp_TienSu instanceof TEMP_TienSuImpl) {
            return temp_TienSu;
        }

        TEMP_TienSuImpl temp_TienSuImpl = new TEMP_TienSuImpl();

        temp_TienSuImpl.setNew(temp_TienSu.isNew());
        temp_TienSuImpl.setPrimaryKey(temp_TienSu.getPrimaryKey());

        temp_TienSuImpl.setTienSuId(temp_TienSu.getTienSuId());
        temp_TienSuImpl.setUserId(temp_TienSu.getUserId());
        temp_TienSuImpl.setUserName(temp_TienSu.getUserName());
        temp_TienSuImpl.setCreateDate(temp_TienSu.getCreateDate());
        temp_TienSuImpl.setModifiedDate(temp_TienSu.getModifiedDate());
        temp_TienSuImpl.setDoiTuongTienSuId(temp_TienSu.getDoiTuongTienSuId());
        temp_TienSuImpl.setToaAnXuPhat(temp_TienSu.getToaAnXuPhat());
        temp_TienSuImpl.setNgayToaAn(temp_TienSu.getNgayToaAn());
        temp_TienSuImpl.setToiDanh(temp_TienSu.getToiDanh());
        temp_TienSuImpl.setNgayPhamToi(temp_TienSu.getNgayPhamToi());
        temp_TienSuImpl.setXoaAnTich(temp_TienSu.isXoaAnTich());
        temp_TienSuImpl.setChuThich(temp_TienSu.getChuThich());
        temp_TienSuImpl.setHinhPhatChinh(temp_TienSu.getHinhPhatChinh());
        temp_TienSuImpl.setHinhPhatBoSung(temp_TienSu.getHinhPhatBoSung());
        temp_TienSuImpl.setNghiaVuDanSu(temp_TienSu.getNghiaVuDanSu());
        temp_TienSuImpl.setAnPhi(temp_TienSu.getAnPhi());
        temp_TienSuImpl.setBanAnSo(temp_TienSu.getBanAnSo());
        temp_TienSuImpl.setMoTaTienAn(temp_TienSu.getMoTaTienAn());
        temp_TienSuImpl.setUndoRedo(temp_TienSu.getUndoRedo());
        temp_TienSuImpl.setThaoTac(temp_TienSu.getThaoTac());
        temp_TienSuImpl.setMainTienSuId(temp_TienSu.getMainTienSuId());

        return temp_TienSuImpl;
    }

    /**
     * Returns the t e m p_ tien su with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
     *
     * @param primaryKey the primary key of the t e m p_ tien su
     * @return the t e m p_ tien su
     * @throws qldc.pm.tiensu.NoSuchTEMP_TienSuException if a t e m p_ tien su with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TEMP_TienSu findByPrimaryKey(Serializable primaryKey)
        throws NoSuchTEMP_TienSuException, SystemException {
        TEMP_TienSu temp_TienSu = fetchByPrimaryKey(primaryKey);

        if (temp_TienSu == null) {
            if (_log.isWarnEnabled()) {
                _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
            }

            throw new NoSuchTEMP_TienSuException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
                primaryKey);
        }

        return temp_TienSu;
    }

    /**
     * Returns the t e m p_ tien su with the primary key or throws a {@link qldc.pm.tiensu.NoSuchTEMP_TienSuException} if it could not be found.
     *
     * @param tienSuId the primary key of the t e m p_ tien su
     * @return the t e m p_ tien su
     * @throws qldc.pm.tiensu.NoSuchTEMP_TienSuException if a t e m p_ tien su with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TEMP_TienSu findByPrimaryKey(long tienSuId)
        throws NoSuchTEMP_TienSuException, SystemException {
        return findByPrimaryKey((Serializable) tienSuId);
    }

    /**
     * Returns the t e m p_ tien su with the primary key or returns <code>null</code> if it could not be found.
     *
     * @param primaryKey the primary key of the t e m p_ tien su
     * @return the t e m p_ tien su, or <code>null</code> if a t e m p_ tien su with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TEMP_TienSu fetchByPrimaryKey(Serializable primaryKey)
        throws SystemException {
        TEMP_TienSu temp_TienSu = (TEMP_TienSu) EntityCacheUtil.getResult(TEMP_TienSuModelImpl.ENTITY_CACHE_ENABLED,
                TEMP_TienSuImpl.class, primaryKey);

        if (temp_TienSu == _nullTEMP_TienSu) {
            return null;
        }

        if (temp_TienSu == null) {
            Session session = null;

            try {
                session = openSession();

                temp_TienSu = (TEMP_TienSu) session.get(TEMP_TienSuImpl.class,
                        primaryKey);

                if (temp_TienSu != null) {
                    cacheResult(temp_TienSu);
                } else {
                    EntityCacheUtil.putResult(TEMP_TienSuModelImpl.ENTITY_CACHE_ENABLED,
                        TEMP_TienSuImpl.class, primaryKey, _nullTEMP_TienSu);
                }
            } catch (Exception e) {
                EntityCacheUtil.removeResult(TEMP_TienSuModelImpl.ENTITY_CACHE_ENABLED,
                    TEMP_TienSuImpl.class, primaryKey);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return temp_TienSu;
    }

    /**
     * Returns the t e m p_ tien su with the primary key or returns <code>null</code> if it could not be found.
     *
     * @param tienSuId the primary key of the t e m p_ tien su
     * @return the t e m p_ tien su, or <code>null</code> if a t e m p_ tien su with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TEMP_TienSu fetchByPrimaryKey(long tienSuId)
        throws SystemException {
        return fetchByPrimaryKey((Serializable) tienSuId);
    }

    /**
     * Returns all the t e m p_ tien sus.
     *
     * @return the t e m p_ tien sus
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<TEMP_TienSu> findAll() throws SystemException {
        return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the t e m p_ tien sus.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link qldc.pm.tiensu.model.impl.TEMP_TienSuModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param start the lower bound of the range of t e m p_ tien sus
     * @param end the upper bound of the range of t e m p_ tien sus (not inclusive)
     * @return the range of t e m p_ tien sus
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<TEMP_TienSu> findAll(int start, int end)
        throws SystemException {
        return findAll(start, end, null);
    }

    /**
     * Returns an ordered range of all the t e m p_ tien sus.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link qldc.pm.tiensu.model.impl.TEMP_TienSuModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param start the lower bound of the range of t e m p_ tien sus
     * @param end the upper bound of the range of t e m p_ tien sus (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of t e m p_ tien sus
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<TEMP_TienSu> findAll(int start, int end,
        OrderByComparator orderByComparator) throws SystemException {
        boolean pagination = true;
        FinderPath finderPath = null;
        Object[] finderArgs = null;

        if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
                (orderByComparator == null)) {
            pagination = false;
            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
            finderArgs = FINDER_ARGS_EMPTY;
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
            finderArgs = new Object[] { start, end, orderByComparator };
        }

        List<TEMP_TienSu> list = (List<TEMP_TienSu>) FinderCacheUtil.getResult(finderPath,
                finderArgs, this);

        if (list == null) {
            StringBundler query = null;
            String sql = null;

            if (orderByComparator != null) {
                query = new StringBundler(2 +
                        (orderByComparator.getOrderByFields().length * 3));

                query.append(_SQL_SELECT_TEMP_TIENSU);

                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
                    orderByComparator);

                sql = query.toString();
            } else {
                sql = _SQL_SELECT_TEMP_TIENSU;

                if (pagination) {
                    sql = sql.concat(TEMP_TienSuModelImpl.ORDER_BY_JPQL);
                }
            }

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                if (!pagination) {
                    list = (List<TEMP_TienSu>) QueryUtil.list(q, getDialect(),
                            start, end, false);

                    Collections.sort(list);

                    list = new UnmodifiableList<TEMP_TienSu>(list);
                } else {
                    list = (List<TEMP_TienSu>) QueryUtil.list(q, getDialect(),
                            start, end);
                }

                cacheResult(list);

                FinderCacheUtil.putResult(finderPath, finderArgs, list);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return list;
    }

    /**
     * Removes all the t e m p_ tien sus from the database.
     *
     * @throws SystemException if a system exception occurred
     */
    @Override
    public void removeAll() throws SystemException {
        for (TEMP_TienSu temp_TienSu : findAll()) {
            remove(temp_TienSu);
        }
    }

    /**
     * Returns the number of t e m p_ tien sus.
     *
     * @return the number of t e m p_ tien sus
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countAll() throws SystemException {
        Long count = (Long) FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
                FINDER_ARGS_EMPTY, this);

        if (count == null) {
            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(_SQL_COUNT_TEMP_TIENSU);

                count = (Long) q.uniqueResult();

                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
                    FINDER_ARGS_EMPTY, count);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
                    FINDER_ARGS_EMPTY);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    /**
     * Initializes the t e m p_ tien su persistence.
     */
    public void afterPropertiesSet() {
        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
                    com.liferay.util.service.ServiceProps.get(
                        "value.object.listener.qldc.pm.tiensu.model.TEMP_TienSu")));

        if (listenerClassNames.length > 0) {
            try {
                List<ModelListener<TEMP_TienSu>> listenersList = new ArrayList<ModelListener<TEMP_TienSu>>();

                for (String listenerClassName : listenerClassNames) {
                    listenersList.add((ModelListener<TEMP_TienSu>) InstanceFactory.newInstance(
                            getClassLoader(), listenerClassName));
                }

                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
            } catch (Exception e) {
                _log.error(e);
            }
        }
    }

    public void destroy() {
        EntityCacheUtil.removeCache(TEMP_TienSuImpl.class.getName());
        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    }
}
